package com.vito.dcs.tendersOverview.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.model.DataGrid;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.dcs.modify.model.DataModifyModel;
import com.vito.dcs.modify.service.DataModifyService;
import com.vito.dcs.tendersOverview.Service.MixingStationService;
import com.vito.dcs.tendersOverview.Service.PileGpsService;
import com.vito.dcs.tendersOverview.Service.TendersOverviewService;
import com.vito.dcs.tendersOverview.model.PileGpsModel;
import com.vito.dcs.tendersOverview.model.StatisticalIntegrationPileKeyModel;
import com.vito.dcs.tendersOverview.model.TendersOverviewModel;
import com.vito.dcs.tendersOverview.model.TotalAsphaltMixingSationModel;
import com.vito.dcs.tendersOverview.model.TotalWaterMixingStationModel;
import com.vito.util.TimeConversionUtil;

/**
 * 
 * 
 * @author zsk
 * @date 2019年4月23日  
 * @version v1.0
 * @Description 标段概况
 *
 */
@Controller
@RequestMapping("/dcs/tendersOverviewAction")
public class TendersOverviewAction extends BaseAction{

	private static final Logger log = LoggerFactory.getLogger(TendersOverviewAction.class);


	@Reference
	private TendersOverviewService tendersOverviewServiceImpl;

	@Reference
	private PileGpsService pileGpsServiceImpl;
	
	@Reference
	private MixingStationService  mixingStationServiceImpl;
	
	@Reference
	private DataModifyService dataModifyService;
	
	/**
	 * 
	 * 
	 * @author zsk
	 * @date 2019年4月23日  
	 * @version v1.0
	 * @Description  标段概况
	 *
	 */
	@RequestMapping("/getTendersProgress")
	@ResponseBody
	public Result getTendersProgress(TendersOverviewModel tendersOverviewModel){
		Result result = new Result();
		//标段ID和工业区分状态必传（01：水稳；02沥青）
			if(tendersOverviewModel.getSectionId() != null && !"".equals(tendersOverviewModel.getSectionId())
				&& tendersOverviewModel.getProcessType() != null && !"".equals(tendersOverviewModel.getProcessType())
			){
			String sectionId = tendersOverviewModel.getSectionId();
			String processType = tendersOverviewModel.getProcessType();
			TendersOverviewModel tom =  new TendersOverviewModel();
				//判断工业分区分类
				if(processType.equals("01")){//水稳01
					try{
						TotalWaterMixingStationModel waterTotal = mixingStationServiceImpl.getWaterPlanTotal(sectionId);
						if(waterTotal != null){
							tom.setPlannedTotal(waterTotal.getSumPlannedTotal() == null ? "0" : waterTotal.getSumPlannedTotal());//计划总量
							
							DataModifyModel dataModifyModel = new DataModifyModel();
							dataModifyModel.setSectionId(sectionId);
							dataModifyModel = dataModifyService.queryDataModifyBySectionId(dataModifyModel);
							
							TotalWaterMixingStationModel waterPlan = mixingStationServiceImpl.getWaterPlanAlreadyTotal(sectionId);
							if(waterPlan != null){
								if(dataModifyModel != null){
									tom.setProducedTotal((Double.parseDouble(waterPlan.getSumAlreadyTotal() == null ? "0" : waterPlan.getSumAlreadyTotal()) + dataModifyModel.getWaterValue()*1000) +"");//已完成总量
								} else {
									tom.setProducedTotal(waterPlan.getSumAlreadyTotal() == null ? "0" : waterPlan.getSumAlreadyTotal());
								}
							} else {
								if(dataModifyModel != null){
									tom.setProducedTotal(dataModifyModel.getWaterValue()*1000 + "");
								}
							}
						}
						tom.setSectionId(sectionId);
						tom.setProcessType(processType);
						result.setCode(Result.CODE_OK);
						result.setData(tom);
					}catch(Exception ex){
						log.error("查询失败",ex);
						result.setCode(Result.CODE_ERROR);
						result.setMsg(ResMsg.EXECERR);
					}
				}else{//沥青02
					try{
						
						TotalAsphaltMixingSationModel asphaltPlanTotal = mixingStationServiceImpl.getAsphaltPlanTotal(sectionId);
						if(asphaltPlanTotal != null){
							tom.setPlannedTotal(asphaltPlanTotal.getSumPlannedTotal());
							DataModifyModel dataModifyModel = new DataModifyModel();
							dataModifyModel.setSectionId(sectionId);
							dataModifyModel = dataModifyService.queryDataModifyBySectionId(dataModifyModel);
							
							TotalAsphaltMixingSationModel asphaltPlanAlreadyTotal = mixingStationServiceImpl.getAsphaltPlanAlreadyTotal(sectionId);
							if(asphaltPlanAlreadyTotal != null){
								if(dataModifyModel != null){
									tom.setProducedTotal((Double.parseDouble(asphaltPlanAlreadyTotal.getSumAlreadyTotal() == null ? "0" : asphaltPlanAlreadyTotal.getSumAlreadyTotal()) + dataModifyModel.getAsphaltValue()*1000) + "");
								} else {
									tom.setProducedTotal(asphaltPlanAlreadyTotal.getSumAlreadyTotal() == null ? "0" : asphaltPlanAlreadyTotal.getSumAlreadyTotal());
								}
							} else {
								if(dataModifyModel != null){
									tom.setProducedTotal(dataModifyModel.getAsphaltValue()*1000 + "");
								}
							}
							
						}
						
						tom.setSectionId(sectionId);
						tom.setProcessType(processType);
						result.setCode(Result.CODE_OK);
						result.setData(tom);
					}catch(Exception ex){
						log.error("查询失败",ex);
						result.setCode(Result.CODE_ERROR);
						result.setMsg(ResMsg.EXECERR);
					}
				}
		}else{
			log.error("查询失败","---标段ID和分类不能为NULL-----");
			result.setCode(Result.CODE_ERROR);
			result.setMsg(ResMsg.EXECERR);
		}
		return result;
	}
     /**
      * 
      * @author zsk
      * @date 2019年4月25日  
      * @version v1.0
      * @Description 拌合站数据统计
      *
      */
	@RequestMapping("/getMixingStationDataStatistics")
	@ResponseBody
	public Result getMixingStationDataStatistics(TendersOverviewModel tendersOverviewModel){
		Result result = new Result();
		if(tendersOverviewModel.getSectionId() != null && !"".equals(tendersOverviewModel.getSectionId()) 
				&& tendersOverviewModel.getProcessType() != null && !"".equals(tendersOverviewModel.getProcessType())){
			String sectionId = tendersOverviewModel.getSectionId();
			String processType = tendersOverviewModel.getProcessType();
			 new TotalWaterMixingStationModel();
			//判断工业分区分类
			if(processType.equals("01")){//水稳01
				try{
					TotalWaterMixingStationModel waterModel = new TotalWaterMixingStationModel();
					waterModel.setMixingName("水稳拌合站");//拌合站名称
					TotalWaterMixingStationModel waterTotal = mixingStationServiceImpl.getWaterPlanTotal(sectionId);
					if(waterTotal != null){
						
						waterModel.setSumPlannedTotal(waterTotal.getSumPlannedTotal() == null ? "0" : waterTotal.getSumPlannedTotal());//计划总量
						
						DataModifyModel dataModifyModel = new DataModifyModel();
						dataModifyModel.setSectionId(sectionId);
						dataModifyModel = dataModifyService.queryDataModifyBySectionId(dataModifyModel);
						
						TotalWaterMixingStationModel waterPlan = mixingStationServiceImpl.getWaterPlanAlreadyTotal(sectionId);
						if(waterPlan != null){
							if(dataModifyModel != null){
								waterModel.setSumAlreadyTotal((Double.parseDouble(waterPlan.getSumAlreadyTotal() == null ? "0" : waterPlan.getSumAlreadyTotal()) + dataModifyModel.getWaterValue()*1000) +"");//已完成总量
							} else {
								waterModel.setSumAlreadyTotal(waterPlan.getSumAlreadyTotal() == null ? "0" : waterPlan.getSumAlreadyTotal());
							}
						} else {
							if(dataModifyModel != null){
								waterModel.setSumAlreadyTotal(dataModifyModel.getWaterValue()*1000 + "");
							}
						}
						
					}
					
					TotalWaterMixingStationModel waterDay = mixingStationServiceImpl.getWaterDayVolume(sectionId);
					if(waterDay != null){
						waterModel.setBatch(waterDay.getBatch() == null ? "0" : waterDay.getBatch());//当日批次
						waterModel.setDataTotal(waterDay.getDataTotal() == null ? "0" : waterDay.getDataTotal());//当日总数
						waterModel.setTime(waterDay.getTime() ==  null ? "" : waterDay.getTime());//最后一次时间
					}

					List<TotalWaterMixingStationModel> list  = new ArrayList<TotalWaterMixingStationModel>();
					list.add(waterModel);
					DataGrid<TotalWaterMixingStationModel> data = new DataGrid<TotalWaterMixingStationModel>();
					data.setRows(list);
					data.setTotal(list.size());
					result.setCode(Result.CODE_OK);
					result.setData(data);
				}catch(Exception ex){
					log.error("查询失败",ex);
					result.setCode(Result.CODE_ERROR);
					result.setMsg(ResMsg.EXECERR);
				}
			}else{//沥青02
				try{
					TotalAsphaltMixingSationModel asphaltModel = new TotalAsphaltMixingSationModel();
					asphaltModel.setMixingName("沥青拌合站");//拌合站名称
					TotalAsphaltMixingSationModel parameDay = mixingStationServiceImpl.getAsphaltDayVolume(sectionId);
					if(parameDay != null){
						asphaltModel.setBatch(parameDay.getBatch() == null ? "0" :parameDay.getBatch() );//当日批次
						asphaltModel.setDataTotal(parameDay.getDataTotal() == null ? "0" : parameDay.getDataTotal());//当日总数
						asphaltModel.setTime(parameDay.getTime() == null ? "" : parameDay.getTime());//最后一次时间
					}
					
					
					TotalAsphaltMixingSationModel asphaltPlanTotal = mixingStationServiceImpl.getAsphaltPlanTotal(sectionId);
					if(asphaltPlanTotal != null){
						
						asphaltModel.setSumPlannedTotal(asphaltPlanTotal.getSumPlannedTotal());
						DataModifyModel dataModifyModel = new DataModifyModel();
						dataModifyModel.setSectionId(sectionId);
						dataModifyModel = dataModifyService.queryDataModifyBySectionId(dataModifyModel);
						
						TotalAsphaltMixingSationModel asphaltPlanAlreadyTotal = mixingStationServiceImpl.getAsphaltPlanAlreadyTotal(sectionId);
						if(asphaltPlanAlreadyTotal != null){
							if(dataModifyModel != null){
								asphaltModel.setSumAlreadyTotal((Double.parseDouble(asphaltPlanAlreadyTotal.getSumAlreadyTotal() == null ? "0" : asphaltPlanAlreadyTotal.getSumAlreadyTotal()) + dataModifyModel.getAsphaltValue()*1000) + "");
							} else {
								asphaltModel.setSumAlreadyTotal(asphaltPlanAlreadyTotal.getSumAlreadyTotal() == null ? "0" : asphaltPlanAlreadyTotal.getSumAlreadyTotal());
							}
						} else {
							if(dataModifyModel != null){
								asphaltModel.setSumAlreadyTotal(dataModifyModel.getAsphaltValue()*1000 + "");
							}
						}
						
					}
					asphaltModel.setSectionId(sectionId);
					List<TotalAsphaltMixingSationModel> list  = new ArrayList<TotalAsphaltMixingSationModel>();
					list.add(asphaltModel);
					DataGrid<TotalAsphaltMixingSationModel> data = new DataGrid<TotalAsphaltMixingSationModel>();
					data.setRows(list);
					data.setTotal(list.size());
					result.setCode(Result.CODE_OK);
					result.setData(data);
				}catch(Exception ex){
					log.error("查询失败",ex);
					result.setCode(Result.CODE_ERROR);
					result.setMsg(ResMsg.EXECERR);
				}
			}
		}else{
			log.error("查询失败","---标段ID和分类不能为NULL-----");
			result.setCode(Result.CODE_ERROR);
			result.setMsg(ResMsg.EXECERR);
		}
		return result;
	}
	
	
}
